/*
 * Copyright (c) 2018-2999 广州市蓝海创新科技有限公司 All rights reserved.
 *
 * https://www.mall4j.com/
 *
 * 未经允许，不可做商业用途！
 *
 * 版权所有，侵权必究！
 */

package com.yami.shop.api.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;

/**
 * @author lanhai
 */
@RestController
@RequestMapping("/p/movie")
@Tag(name = "电影接口")
@AllArgsConstructor
public class MovieController {

    /**
     * 首页轮播图接口
     *
     * @return
     */
    @PostMapping("/getSign")
    @Operation(summary = "获取签名", description = "获取签名")
    public String getSign() throws IOException {

        InputStream in = null;
        String result = "";
        String appid = "10000000000";
        String appSecret = "0f73ec201b877338541a54f49394bccb";
        long epochSecond = LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8"));
        String urlStr = appid + epochSecond + appSecret ;
        String signature = DigestUtils.md5DigestAsHex(urlStr.getBytes());
        System.out.println(signature);
        String urlString = "https://test.tuzyp.com/api/movie/auth/accesstoken"; // 替换为你的API URL

        String boundary = Long.toHexString(System.currentTimeMillis());
        String CRLF = "\r\n"; // Carriage return/new line
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();

        connection.setDoOutput(true);
        connection.setRequestMethod("POST");
        connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        try (
                OutputStream output = connection.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);
        ) {
            // 发送 JSON 数据
            String json = "{\"appId\":\""+appid+"\",\"timestamp\":\""+epochSecond+"\",\"sign\"\""+signature+"\"}";
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"json\"").append(CRLF);
            writer.append("Content-Type: application/json").append(CRLF);
            writer.append(CRLF).append(json).append(CRLF);
            writer.flush();

            // 结束边界
            writer.append("--" + boundary + "--").append(CRLF);
        }

        int responseCode = connection.getResponseCode();
        System.out.println("Response Code: " + responseCode);
        //获取URLConnection对象对应的输入流
        in = connection.getInputStream();
        //解析对方服务器返回的数据
        result = readInputStream(in);


        // 处理响应...

        connection.disconnect();
        return result;
    }
    //解析对方服务器返回流
    public static String readInputStream(InputStream in) throws IOException {
        byte[] b=new byte[1024];
        StringBuffer sb1=new StringBuffer();
        int length=0;
        while ((length=in.read(b))!=-1){
            String s=new String(b, 0,length);
            sb1.append(s);
        }
        return sb1.toString();
    }

}
